local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();

instStr={
    
    sellCount ="ctrl.Delete.count",--售卖数量
    selectAll ="ctrl.Delete.selectAll",--选中全部售卖
    cancel ="ctrl.Delete.cancel",--取消售卖
    sureSell="ctrl.Delete.sure",--确认售卖
}

function M.new(...)
	return M(...);
end;


function M.ctor(self,ctrl)
	M.super.ctor(self);

    self.SellEvent=ConnectClass.new();

    self.ctrl=ctrl
    self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)
    
    CallBackManager:AddRbxConnect(self,self.ui.cancel.Activated, function()
        self.ctrl:ShowOnlyStatu("equip")
	end);


    CallBackManager:AddRbxConnect(self,self.ui.sureSell.Activated, function()
        self.ctrl.SellEvent:Action(self:PullSellDataId())
        self.ctrl:ShowOnlyStatu("equip")
	end);


    CallBackManager:AddRbxConnect(self,self.ui.selectAll.Activated, function()
            self:SelectAll()
	end);

end;


function M.dtor(self)
	M.super.dtor(self);
end;


--返回售卖数据
function M.PullSellDataId(self)
    local list={}
    for _,cell in pairs(self.ctrl:GetAllCell()) do
        if cell:isSelling() then
            table.insert(list,cell:GetPetId())
        end
    end
    return list
end

-- 清除数据
function M.Clear(self)
    for _,cell in pairs(self.ctrl:GetAllCell()) do
        cell:SetSellStatus(false)
    end
    self.ctrl:FullText(self.ui.sellCount,{0})
end

function M.IsShowing(self)
    return  self.isShow
end


function M.Show(self)
    self.ui.sellCount.Visible=true
    self.ui.selectAll.Visible=true
    self.ui.cancel.Visible=true
    self.ui.sureSell.Visible=true
    self.isShow=true
end


function M.Hide(self)
    self.ui.sellCount.Visible=false
    self.ui.selectAll.Visible=false
    self.ui.cancel.Visible=false
    self.ui.sureSell.Visible=false
    self.isShow=nil
    self:Clear()
end


function M.Update(self)
    local count=0
    for _,cell in pairs(self.ctrl:GetAllCell()) do
        if cell:isSelling() then
            count=count+1
        end
    end
    self.ctrl:FullText(self.ui.sellCount,{count})
   
end


function M.isCanSell(self,cell)
    if cell:isLock() or cell:isEquiped() then
        return false
    end
    return true
end


function M.SelectAll(self)
    for _,cell in pairs(self.ctrl:GetAllCell()) do
        if self:isCanSell(cell) then
            cell:SetSellStatus(true)
        end
    end
    self:Update()
end

function M.Change(self,cell)
    if self:isCanSell(cell) then
        cell:ChangeSell()
        self:Update()
    end
end


return M